package com.zxl.always.on.display.tools;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;

import com.zxl.always.on.display.constant.LockerEnv;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2018/3/2.
 */

public class BitmapUtils {

    public static Bitmap getRoundedCornerBitmap(Context paramContext, Bitmap paramBitmap, int round) {
        Bitmap bitmap = Bitmap.createBitmap(paramBitmap.getWidth(), paramBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, paramBitmap.getWidth(), paramBitmap.getHeight());
        RectF rectF = new RectF(rect);
        float f = convertDipsToPixels(paramContext, round);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(-16777216);
        canvas.drawRoundRect(rectF, f, f, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(paramBitmap, rect, rect, paint);
        return bitmap;
    }

    public static int convertDipsToPixels(Context paramContext, int paramInt) {
        return (int) (0.5F + paramContext.getResources().getDisplayMetrics().density * paramInt);
    }


    public static Bitmap createBitmap(Context context, Intent data) {
        InputStream inputStream = null;
        try {
            Uri uri = data.getData();
            inputStream = context.getContentResolver().openInputStream(uri);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            BitmapFactory.decodeStream(inputStream, null, options);
            int originalWidth = options.outWidth;
            int originalHeight = options.outHeight;
            if (originalHeight == -1 || originalWidth == -1) return null;
            float height = 1280f;
            float width = 960f;
            int noCompress = 1;
            if (originalWidth > originalHeight && originalWidth > width) {
                noCompress = (int) (originalWidth / width);
            } else if (originalWidth < originalHeight && originalHeight > height) {
                noCompress = (int) (originalHeight / height);
            }
            if (noCompress <= 0) noCompress = 1;
            BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
            bitmapOptions.inSampleSize = noCompress;
            options.inJustDecodeBounds = true;
            bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
            inputStream = context.getContentResolver().openInputStream(uri);

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, bitmapOptions);
            return compressBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    private static Bitmap compressBitmap(Bitmap bitmap) throws IOException {
        int options = 100;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);  //先不压缩，数据保存到outputStream；//主要还是采用的则会个方法
        while (outputStream.toByteArray().length / 1024 > 350) {//循环判断如果压缩后图片是否大于400kb,大于继续压缩
            outputStream.reset(); //重置
            //第一个参数 ：图片格式 ，第二个参数： 图片质量，100为最高，0为最差  ，第三个参数：保存压缩后的数据的流
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, outputStream);
            options -= 10;//每次都减少10
        }
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        Bitmap decodeStream = BitmapFactory.decodeStream(inputStream, null, null);
        //保存为resultFile的对象
        try {
            File file = new File(LockerEnv.Path.IMAGE_TMP);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(LockerEnv.Path.IMAGE_TMP));
            decodeStream.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return decodeStream;
    }


}
